package com.example.homework.login;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.example.homework.R;
import com.example.homework.db.UserDbHelper;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;

public class RegisterActivity extends AppCompatActivity {
    private EditText userEditText;
    private EditText pwoEditText;
    private EditText rpwEditText;
    private EditText unameEditText;
    private CheckBox checkBoxAgreement;
    private Button buttonRegister;
    private Button buttonBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        // 通过findViewById方法找到布局文件中的对应组件
        userEditText = findViewById(R.id.user);
        pwoEditText = findViewById(R.id.pwo);
        rpwEditText = findViewById(R.id.rpw);
        unameEditText = findViewById(R.id.uname);
        checkBoxAgreement = findViewById(R.id.checkBox3);
        buttonRegister = findViewById(R.id.buttonR);
        buttonBack = findViewById(R.id.buttonB);

        // 设置注册按钮的点击事件监听器
        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ShowToast")
            @Override
            public void onClick(View v) {
                if (checkBoxAgreement.isChecked()) {
                    String username = userEditText.getText().toString().trim();
                    String password = pwoEditText.getText().toString().trim();
                    String repeatPassword = rpwEditText.getText().toString().trim();
                    String name = unameEditText.getText().toString().trim();

                    if (username.isEmpty() || password.isEmpty() || repeatPassword.isEmpty() || name.isEmpty()) {
                        Toast.makeText(RegisterActivity.this, "请填写所有内容", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if (!password.equals(repeatPassword)) {
                        Toast.makeText(RegisterActivity.this, "两次密码输入不一致，请重新输入", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    // 将用户信息插入数据库
                    UserDbHelper dbHelper = new UserDbHelper(RegisterActivity.this);
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put(UserDbHelper.COLUMN_USERNAME, username);
                    values.put(UserDbHelper.COLUMN_PASSWORD, password);
                    values.put(UserDbHelper.COLUMN_NAME, name);
                    // 这里假设性别等其他信息可以后续完善添加，暂时先不处理具体赋值逻辑
                    long result = db.insert(UserDbHelper.TABLE_NAME, null, values);
                    if (result!= -1) {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(RegisterActivity.this, "注册失败，请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                    db.close();
                    dbHelper.close();
                } else {
                    Toast.makeText(RegisterActivity.this, "请阅读会员协议并勾选", Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 设置返回按钮的点击事件监听器，返回到登录界面
        buttonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}